package com.zhongjian.technologycal.mall.activity.main.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.zhongjian.technologycal.mall.activity.personal.SettingsActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.ValidatorUtils;

import io.github.xudaojie.qrcodelib.CaptureActivity;
import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by wei on 17/9/17.
 */

public class SimpleCaptureActivity extends CaptureActivity {
    protected Activity mActivity = this;
    private MaterialDialog materialDialog = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        mActivity = this;
        super.onCreate(savedInstanceState);
    }
    @Override
    protected void handleResult(final String resultString) {
        if (TextUtils.isEmpty(resultString)) {
            ToastUitl.showShort("失败");
            restartPreview();
        } else {
            if (materialDialog == null) {
                materialDialog = new MaterialDialog(SimpleCaptureActivity.this)
                        .setMessage(resultString)
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(ValidatorUtils.isUrl(resultString)){
                                    Intent intent = new Intent();
                                    intent.setAction("android.intent.action.VIEW");
                                    Uri content_url = Uri.parse(resultString);
                                    intent.setData(content_url);
                                    startActivity(intent);
                                }else{
                                   finish();
                                }
                                materialDialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                restartPreview();
//                                finish();
                                materialDialog.dismiss();
                            }
                        });
                materialDialog.setCanceledOnTouchOutside(true);
                materialDialog.show();
            }

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        restartPreview();
    }
}
